const activityModal = require('./../modal/activityModal.js');
const XLSXWriter = require('xlsx-writestream');
const xlsx = require('node-xlsx');
const fs = require('fs');
const path = require('path');

const commonJs = require('../commonMethod/common.js')
module.exports = {
		getActivityList: function (request,reponse) {
			activityModal.getActivityList(request.body,request.query,function (err,data) {
				if(err){
					reponse.send({code:400,data:err});
				}else{
		        	reponse.send({code:200,data:data});
				}
	        });
	    },
	    saveActivity: function (request,reponse) {
	    	let params = request.body;
	    	params.createTime = new Date();
	    	delete params.cityLocation;
	    	
			activityModal.saveActivity(params,request.query,function (err,data) {
				if(err){
					reponse.send({code:400,data:err});
				}else{
					reponse.send({code:200,data:data});
				}
	            
	        });
	    },
	    changeActivity: function (request,reponse){
	    	let params = request.body;
	    	delete params.cityLocation;
	    	
	    	activityModal.changeActivity(params,function (err,data) {
	    		if(err){
					reponse.send({code:400,data:err});
				}else{
					reponse.send({code:200,data:data});
				}
	        });
	    },
	    getActivityInfo:function(request,reponse){
	    	activityModal.getActivityInfo(request.params,function (err,data) {
	    		if(err){
					reponse.send({code:400,data:err});
				}else{
					reponse.send({code:200,data:data});
				}
	        });
	    },
	    exportActivityList:function(request,reponse){
	    	activityModal.exportActivityList(request.params,function (err,data) {
	    		
	    		//文件导出时的路径
	    	    const filepath = './PRIZE.xlsx';
	    	    console.log('^^^^^^filepath',filepath);

	    	    const writer = new XLSXWriter(filepath, {} /* options */);
	    	    //这里就用到了流
	    	    const wirteStream = fs.createWriteStream(filepath);           

	    	    writer.getReadStream().pipe(wirteStream);
	    	 		//循环传进来的对象，excel表里需要的字段
	    	 		for(let i = 0;i<data.length;i++){
	    	 			try{
	    	               	writer.addRow({
	    	 	                'id': data[i].id,
	    	 	                '团队号码': data[i].teamNo,
	    	 	                '开始时间': data[i].startDate,
	    	 	                '结束时间': data[i].endDate,
	    	 	                '地点': data[i].location,
	    	 	                '发票': data[i].invoice,
	    	                    '客户公司': data[i].client,
	    	 	                '客户负责人': data[i].clientCharge,
	    	 	                '发票金额（含增值税）': data[i].invoiceAmount,
	    	 	                '增值税': data[i].VAT,
	    	 	                '实际费用（不含增值税，含服务费）':data[i].VAT,
	    	 	                '成本':data[i].actualCost,
	    	 	                '利润额':data[i].cost,
	    	 	                '利润率':data[i].profit,
	    	 	                '操作负责人':data[i].charge,
	    	 	                '出团人员':data[i].participant,
	    	 	                'po号':data[i].PONo,
	    	 	                '报账日期':data[i].contract,
	    	 	                '开票日期':data[i].reportDate,
	    	 	                '到账日期':data[i].billingDate,
	    	 	                '备注':data[i].remark
	    	               	});
	    	             }catch(e){
	    	               	console.log(e);
	    	             };
	    	 		}
	    	 		writer.finalize();
	    	 		wirteStream.on('finish', function () {
	    	 			let reg = new RegExp("/acs/code");
	    	 			let currpath = filepath.replace(reg,"");
	    	             if(err){
    						reponse.send({code:400,data:err});
    					}else{
    						reponse.send({code:200,data:path.resolve(currpath)});
    					}
	    	             setTimeout(function(){
	    	            	 fs.unlinkSync(path.resolve(currpath));
	    	             },1000*20);
	    	            
	    	         });

	
	    		
	        });
	    },
};